#include"Data.h"

MYSQL* aod::MysqlInit()
{
    MYSQL* mysql = mysql_init(NULL);
    if(mysql == NULL)
    {
        std::cout << "init mysql failed\n" << std::endl;
        return NULL;
    }

    if(mysql_real_connect(mysql,HOST,USER,PASSWARD,NAME,0,NULL,0) == NULL)
    {
        std::cout << "connect mysql failed\n" << std::endl;
        mysql_close(mysql);
        return NULL;
    }
    mysql_set_character_set(mysql,"utf8");
    return mysql;
}

void aod::MysqlDestory(MYSQL* mysql)
{
    if(mysql != NULL)
    {
        mysql_close(mysql);
    }
    return;
}

bool aod::MysqlQuery(MYSQL* mysql,const std::string& sql)
{
    int ret = mysql_query(mysql,sql.c_str());
    if(ret != 0)
    {
        std::cout << sql << "  " << mysql_error(mysql) << std::endl;
        return false;
    }
    return true;
}

bool aod::TableVideo::Insert(const Json::Value& video)
{
    //id name info video image
    std::string sql;
    sql.resize(4096+video["info"].asString().size());   //防止简介过长
    #define INSERT_VIDEO "insert table_video values(null,'%s','%s','%s','%s');"

    if(video["name"].asString().length() == 0)
    {
        std::cout << "name error" << std::endl;
        return false;
    }
    if(video["info"].asString().length() == 0)
    {
        std::cout << "info error" << std::endl;
        return false;
    }
    if(video["video"].asString().length() == 0)
    {
        std::cout << "video error" << std::endl;
        return false;
    }
    if(video["image"].asString().length() == 0)
    {
        std::cout << "image error" << std::endl;
        return false;
    }
    sprintf(&sql[0],INSERT_VIDEO,
            video["name"].asCString(),
            video["info"].asCString(),
            video["video"].asString().c_str(),
            video["image"].asCString());
    return MysqlQuery(_mysql,sql);
}

bool aod::TableVideo::UpDate(int video_id,const Json::Value& video)
{
    std::string sql;
    sql.resize(4096+video["info"].asString().size());   //防止简介过长
    #define UPDATE_VIDEO "update table_video set name = '%s', info = '%s' where id = '%d';"
    
    if(video["name"].asString().length() == 0)
    {
        std::cout << "name error" << std::endl;
        return false;
    }
    if(video["info"].asString().length() == 0)
    {
        std::cout << "info error" << std::endl;
        return false;
    }
    sprintf(&sql[0],UPDATE_VIDEO,
            video["name"].asCString(),
            video["info"].asCString(),
            video_id);
    return MysqlQuery(_mysql,sql);
}

bool aod::TableVideo::Delete(const int video_id)
{
    #define DELETE_VIDEO "delete from table_video where id = '%d';"
    char sql[1024] = {0};
    sprintf(&sql[0],DELETE_VIDEO,video_id);
    return MysqlQuery(_mysql,sql);
}

bool aod::TableVideo::SelectAll(Json::Value* videos)
{
    #define SELECTALL "select * from table_video;"
    _mtx.lock();
    bool ret = MysqlQuery(_mysql,SELECTALL);
    if(ret == false)
    {
        _mtx.unlock();
        return false;
    }   
    MYSQL_RES* res =  mysql_store_result(_mysql);
    if(res == NULL)
    {
        std::cout << "mysql store result failed" << std::endl;
        _mtx.unlock();
        return false;
    }
    _mtx.unlock();

    int num_cows = mysql_num_rows(res);
    for(int i = 0;i < num_cows;i++)
    {
        MYSQL_ROW row = mysql_fetch_row(res);
        Json::Value video;
        video["id"] = atoi(row[0]);
        video["name"] = row[1];
        video["info"] = row[2];
        video["video"] = row[3];
        video["image"] = row[4];
        video["like_count"] = (row[5] != NULL) ? atoi(row[5]) : 0;
        videos->append(video);
    }
    mysql_free_result(res);
    return true;
}

bool aod::TableVideo::SelectOne(const int video_id,Json::Value* video)
{
    #define SELECTONE "select * from table_video where id = %d;"
    char sql[1024] = {0};
    sprintf(sql,SELECTONE,video_id);
    _mtx.lock();
    bool ret = MysqlQuery(_mysql,sql);
    if(ret == false)
    {
        _mtx.unlock();
        return false;
    }   
    MYSQL_RES* res =  mysql_store_result(_mysql);
    if(res == NULL)
    {
        std::cout << "mysql store result failed" << std::endl;
        _mtx.unlock();
        return false;
    }
    _mtx.unlock();

    int num_cows = mysql_num_rows(res);
    if(num_cows != 1)
    {
        std::cout << "have no data\n" << std::endl;
        mysql_free_result(res);
        return false;
    }
    MYSQL_ROW row = mysql_fetch_row(res);
    (*video)["id"] = video_id;
    (*video)["name"] = row[1];
    (*video)["info"] = row[2];
    (*video)["video"] = row[3];
    (*video)["image"] = row[4];
    (*video)["like_count"] = (row[5] != NULL) ? atoi(row[5]) : 0;
    
    mysql_free_result(res);
    return true;
}

bool aod::TableVideo::SelectLike(const std::string& key,Json::Value* videos)
{
    #define SELECTLIKE "select * from table_video where name like '%%%s%%';"
    char sql[1024] = {0};
    sprintf(sql,SELECTLIKE,key.c_str());
    _mtx.lock();
    bool ret = MysqlQuery(_mysql,sql);
    if(ret == false)
    {
        _mtx.unlock();
        return false;
    }   
    MYSQL_RES* res =  mysql_store_result(_mysql);
    if(res == NULL)
    {
        std::cout << "mysql store result failed" << std::endl;
        _mtx.unlock();
        return false;
    }
    _mtx.unlock();

    int num_cows = mysql_num_rows(res);
    for(int i = 0;i < num_cows;i++)
    {
        MYSQL_ROW row = mysql_fetch_row(res);
        Json::Value video;
        video["id"] = atoi(row[0]);
        video["name"] = row[1];
        video["info"] = row[2];
        video["video"] = row[3];
        video["image"] = row[4];
        video["like_count"] = (row[5] != NULL) ? atoi(row[5]) : 0;
        videos->append(video);
    }
    mysql_free_result(res);
    return true;
}



          